package com.slingshot.identitylibrary

import android.content.Context
import android.graphics.Bitmap
import com.slingshot.identitylibrary.idinfo.agent.IdInfoAgent
import com.slingshot.identitylibrary.idinfo.callback.IDInfoCallback
import com.slingshot.identitylibrary.scan.agent.IDScanAgent
import java.io.File
import java.lang.Exception

/**
 * @author liu shuai
 * 描述：弹弓科技SDK工具类
 */
object Slingshot {

    private lateinit var context: Context
    private lateinit var idScanAgent: IDScanAgent
    private lateinit var idInfoAgent: IdInfoAgent

    private var appId: String = ""
    private var appSecret: String = ""

    @JvmStatic
    fun init(context: Context, appId: String, appSecret: String) {
        this.context = context
        this.appId = appId
        this.appSecret = appSecret
        idScanAgent = IDScanAgent(context)
        idInfoAgent = IdInfoAgent()
    }

    @JvmStatic
    fun idScanAgent(): IDScanAgent {
        return idScanAgent
    }

    @JvmStatic
    fun extractIdCardFace(bitmap: Bitmap, extractIdCardCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractIdCardCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        val imageBase64 = SlingshotUtil.bitmapToBase64(bitmap)
        extractIdCardFace(imageBase64, extractIdCardCallback)
    }

    @JvmStatic
    fun extractIdCardFace(file: File, extractIdCardCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractIdCardCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        val imageBase64 = SlingshotUtil.fileToBase64(file)
        extractIdCardFace(imageBase64, extractIdCardCallback)
    }

    @JvmStatic
    fun extractIdCardFace(imageBase64: String, extractIdCardCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractIdCardCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        idInfoAgent.extractIdCardFace(appId, appSecret, imageBase64, extractIdCardCallback)
    }

    @JvmStatic
    fun extractIdCardNationalEmblem(bitmap: Bitmap, extractIdCardCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractIdCardCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        val imageBase64 = SlingshotUtil.bitmapToBase64(bitmap)
        extractIdCardNationalEmblem(imageBase64, extractIdCardCallback)
    }

    @JvmStatic
    fun extractIdCardNationalEmblem(file: File, extractIdCardCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractIdCardCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        val imageBase64 = SlingshotUtil.fileToBase64(file)
        extractIdCardNationalEmblem(imageBase64, extractIdCardCallback)
    }

    @JvmStatic
    fun extractIdCardNationalEmblem(imageBase64: String, extractIdCardCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractIdCardCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        idInfoAgent.extractIdCardNationalEmblem(
            appId,
            appSecret,
            imageBase64,
            extractIdCardCallback
        )
    }

    @JvmStatic
    fun extractFaceDetect(bitmap: Bitmap, extractFaceCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractFaceCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        val imageBase64 = SlingshotUtil.bitmapToBase64(bitmap)
        extractFaceDetect(imageBase64, extractFaceCallback)
    }

    @JvmStatic
    fun extractFaceDetect(file: File, extractFaceCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractFaceCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        val imageBase64 = SlingshotUtil.fileToBase64(file)
        extractFaceDetect(imageBase64, extractFaceCallback)
    }

    @JvmStatic
    fun extractFaceDetect(imageBase64: String, extractFaceCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractFaceCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        idInfoAgent.extractFaceDetect(appId, appSecret, imageBase64, extractFaceCallback)
    }
    @JvmStatic
    fun extractFaceDetect(file: File, file_id: File,extractFaceCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractFaceCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        val imageBase64 = SlingshotUtil.fileToBase64(file)
        val imageBase64_ID = SlingshotUtil.fileToBase64(file_id)
        extractFaceDetect(imageBase64,imageBase64_ID, extractFaceCallback)
    }

    @JvmStatic
    fun extractFaceDetect(imageBase64: String, imageBase64_ID: String,extractFaceCallback: IDInfoCallback) {
        if (checkPermission()) {
            extractFaceCallback.extractFail(Exception("SlingshotSDK : appId or appSecret Error!"))
            return
        }
        idInfoAgent.extractFaceDetect(appId, appSecret, imageBase64,imageBase64_ID, extractFaceCallback)
    }

    private fun checkPermission(): Boolean {
        return appId.isEmpty() || appSecret.isEmpty()
    }

    @JvmStatic
    fun release() {
        idScanAgent.release()
        idInfoAgent.release()
    }
}